﻿using Ext.Net;

namespace KamLib
{

    public interface IRecordWindowContainer : IEquipContainer
    {
        Window Wnd { get; }
        ValidationStatus ValidStatus { get; set; }
        bool CanReload { get; }
        bool CanSave { get; }
        Button cmdReload { get; set; }
        Button cmdApply { get; set; }
        Button cmdOK { get; set; }
        Button cmdClose { get; set; }
    }


    public abstract class RecordWindowContainer : EquipContainer, IRecordWindowContainer
    {
        public Window Wnd { get; set; }

        public ValidationStatus ValidStatus { get; set; }
        public abstract bool CanReload { get; }
        public abstract bool CanSave { get; }
        public Button cmdReload { get; set; }
        public Button cmdApply { get; set; }
        public Button cmdOK { get; set; }
        public Button cmdClose { get; set; }
    }


    public class RecordWindowLogicContainer : RecordWindowContainer
    {
        public RecordWindowLogicContainer(FormLogic logic) { Logic = logic; }
        public FormLogic Logic;
        public override bool CanReload { get { return Logic.CanReload; } }
        public override bool CanSave { get { return Logic.CanSave; } }
    }

    public static class RecordWindowEquiper
    {
        public static void Apply(IRecordWindowContainer c, bool creating)
        {
            AddFooter(c, creating);
        }

        private static void AddFooter(IRecordWindowContainer c, bool creating)
        {
            if (!creating) return;

            var wnd = c.Wnd;

            #region create footer

            StatusBar sbar;
            HBoxLayout btnBox;
            var footer = new Container
            {
                Layout = "Column",
                Height = 40,
                AnchorHorizontal = "0",
                Items = 
                {
                    new Container 
                    {
                        ColumnWidth = 1,
                        Items =
                        {
                            new HBoxLayout 
                            {
                                Align = HBoxAlign.Middle,
                                BoxItems =
                                {
                                    new BoxItem 
                                    {
                                        Margins = "8 0 0 0",
                                        Items =
                                        {
                                            (sbar = new StatusBar
                                            {
                                                Flat = true,
                                                StatusAlign = StatusAlign.Right,
                                                Flex = 1, 
                                                Height = 32,
                                            })
                                        }   
                                    }
                                }
                            }
                        }
                    },
                    new Container 
                    {
                        Width=190,
                        Items =
                        {
                            (btnBox = new HBoxLayout 
                            {
                                Align = HBoxAlign.Middle,
                                Pack = BoxPack.End,
                            })
                        }
                    }
                }
            };

            #endregion


            if (c.CanReload)
            {
                sbar.Items.Add(c.cmdReload = new Button { ID = "cmdReload", Icon = Icon.Reload, ToolTip = "Restore previously saved data", });
                sbar.Items.Add(new ToolbarSeparator());
            }

            if (wnd.Buttons != null && wnd.Buttons.Count>0)
            {
                sbar.Items.AddRange(wnd.Buttons);
                wnd.Buttons.Clear();
            }

            if (c.CanSave)
            {
                sbar.Plugins.Add(
                    c.ValidStatus = new ValidationStatus
                    {
                        ID = "ValidStatus",
                        //FormPanelID = _Form.ID,
                        ValidIconCls = "icon-check-24 x-h24 x-padding-left-32",
                        ErrorIconCls = "icon-error-24 x-h24 x-padding-left-32",
                        ShowText = "",
                        HideText = ""
                    }
                );
            
                sbar.Items.Add(c.cmdApply = new Button { ID = "cmdApply", Icon = Icon.Disk, ToolTip = "Save data without closing the window", });
                btnBox.BoxItems.Add(new BoxItem
                {
                    Margins = "4 0 0 0",
                    Items = { (c.cmdOK = new Button { ID = "cmdOK", Text = "Save & Close", Scale = ButtonScale.Medium, Width = 100, }) }
                });
            }

            btnBox.BoxItems.Add(
                new BoxItem
                {
                    Margins = "4 4 0 10",
                    Items = { (c.cmdClose = new Button { Text = "Close", Scale = ButtonScale.Medium, Width = 70, }) }
                }
            );

            c.Wnd.Items.Add(footer);
        }

    }
}